package com.internetCafes.spms.web.sys.controller;

import com.internetCafes.spms.common.CommonConstants;
import com.internetCafes.spms.common.utils.ShiroUtils;
import com.internetCafes.spms.core.annotation.SysLog;
import com.internetCafes.spms.core.exception.RRException;
import com.internetCafes.spms.core.utils.R;
import com.internetCafes.spms.web.sys.bizservice.MenuInfoBizService;
import com.internetCafes.spms.web.sys.entity.vo.MenuNavEntityVO;
import com.internetCafes.spms.web.sys.entity.vo.MenuNavVO;
import com.internetCafes.spms.web.sys.model.MenuInfo;
import com.internetCafes.spms.web.sys.service.MenuInfoService;
import com.internetCafes.spms.web.sys.service.ShiroService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


/**
 * 菜单管理
 *
 * @author Sunny
 * @email rekeeper2011@hotmail.com
 * @date 2019-10-29 00:11:51
 */
@Api(tags = {"系统菜单接口"})
@RestController
@RequestMapping("/sys/menu")
public class MenuInfoController {

    @Autowired
    private MenuInfoService menuInfoService;
    @Autowired
    private MenuInfoBizService menuInfoBizService;

    @Autowired
    private ShiroService shiroService;

    @ApiOperation(value = "加载导航菜单")
    @GetMapping("/nav")
    public MenuNavEntityVO nav() {
        List<MenuNavVO> menuList = menuInfoService.getUserMenuListAndPermissions(ShiroUtils.getUserId());
        Set<String> permissions = shiroService.getUserPermissions(ShiroUtils.getUserId());
        return new MenuNavEntityVO(0, menuList, permissions);
    }

	/*@ApiOperation(value = "加载导航菜单")
	@GetMapping("/nav")
	public R nav(){
		List<MenuInfo> menuList = menuInfoService.getUserMenuList(ShiroUtils.getUserId());
		Set<String> permissions = shiroService.getUserPermissions(ShiroUtils.getUserId());
		return R.ok().put("menuList", menuList).put("permissions", permissions);
	}*/

    /**
     * 所有菜单列表
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:menu:list")
    public List<MenuInfo> list() {
        return menuInfoBizService.list();
    }

    /**
     * 选择菜单(添加、修改菜单)
     */
    @GetMapping("/select")
    @RequiresPermissions("sys:menu:select")
    public R select() {
        //查询列表数据
        List<MenuInfo> menuList = menuInfoService.queryNotButtonList();

        //添加顶级菜单
        MenuInfo root = new MenuInfo();
        root.setMenuId(0L);
        root.setName("一级菜单");
        root.setParentId(-1L);
        root.setOpen(true);
        menuList.add(root);

        return R.ok().put("menuList", menuList);
    }

    /**
     * 菜单信息
     */
    @GetMapping("/info/{menuId}")
    @RequiresPermissions("sys:menu:info")
    public R info(@PathVariable("menuId") Long menuId) {
        MenuInfo menu = menuInfoService.getById(menuId);
        return R.ok().put("menu", menu);
    }

    /**
     * 保存
     */
    @SysLog("保存菜单")
    @PostMapping("/save")
    @RequiresPermissions("sys:menu:save")
    public R save(@RequestBody MenuInfo menu) {
        //数据校验
        verifyForm(menu);

        menuInfoService.save(menu);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改菜单")
    @PostMapping("/update")
    @RequiresPermissions("sys:menu:update")
    public R update(@RequestBody MenuInfo menu) {
        //数据校验
        verifyForm(menu);

        menuInfoService.updateById(menu);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除菜单")
    @PostMapping("/delete/{menuId}")
    @RequiresPermissions("sys:menu:delete")
    public R delete(@PathVariable("menuId") long menuId) {
        if (menuId <= 31) {
            return R.error("系统菜单，不能删除");
        }

        //判断是否有子菜单或按钮
        List<MenuInfo> menuList = menuInfoService.queryListParentId(menuId);
        if (menuList.size() > 0) {
            return R.error("请先删除子菜单或按钮");
        }

        menuInfoService.delete(menuId);

        return R.ok();
    }

    /**
     * 验证参数是否正确
     */
    private void verifyForm(MenuInfo menu) {
        if (StringUtils.isBlank(menu.getName())) {
            throw new RRException("菜单名称不能为空");
        }

        if (menu.getParentId() == null) {
            throw new RRException("上级菜单不能为空");
        }

        //菜单
        if (menu.getType() == CommonConstants.FunctionType.MENU.getValue()) {
            if (StringUtils.isBlank(menu.getUrl())) {
                throw new RRException("菜单URL不能为空");
            }
        }

        //上级菜单类型
        int parentType = CommonConstants.FunctionType.CATALOG.getValue();
        if (menu.getParentId() != 0) {
            MenuInfo parentMenu = menuInfoService.getById(menu.getParentId());
            parentType = parentMenu.getType();
        }

        //目录、菜单
        if (menu.getType() == CommonConstants.FunctionType.CATALOG.getValue() ||
                menu.getType() == CommonConstants.FunctionType.MENU.getValue()) {
            if (parentType != CommonConstants.FunctionType.CATALOG.getValue()) {
                throw new RRException("上级菜单只能为目录类型");
            }
            return;
        }

        //按钮
        if (menu.getType() == CommonConstants.FunctionType.BUTTON.getValue()) {
            if (parentType != CommonConstants.FunctionType.MENU.getValue()) {
                throw new RRException("上级菜单只能为菜单类型");
            }
            return;
        }
    }

}
